package com.qqtech.qquan.idle.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.agent.context.AdminPage;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.idle.enums.IdleBaseStatusDictEnum;
import com.qqtech.qquan.idle.model.IdleBase;
import com.qqtech.qquan.idle.model.IdleBaseQuery;
import com.qqtech.qquan.idle.model.IdleBaseVo;
import com.qqtech.qquan.idle.service.IdleBaseService;
import com.qqtech.qquan.sys.controller.BaseAgentController;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;

@Controller
@RequestMapping(value = "/idle/base")
public class IdleBaseController extends BaseAgentController {

	@Resource
	private IdleBaseService idleBaseService;
	
	@Resource
	private UserBaseService userBaseService;
	
	@Resource
	private CommunityBaseService communityBaseService;
	
	@Override
	protected BaseService<IdleBase> getBaseService() {
		return idleBaseService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/idle/idleBase-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(IdleBaseQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<IdleBaseVo> page = idleBaseService.queryPageList(query, pageKit);
		UserBaseVo userBaseVo=null;
		CommunityBaseVo communityBaseVo=null;
		for (IdleBaseVo idleBaseVo : page) {
			userBaseVo=userBaseService.getById(idleBaseVo.getCreatorId());
			idleBaseVo.setUserNick(userBaseVo.getNick());
			communityBaseVo=communityBaseService.getById(idleBaseVo.getCommunityId());
			idleBaseVo.setCommunityName(communityBaseVo.getName());
		}
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	
	/**
	 * 恢复
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/receive")
	@ResponseBody
	public Result receive(Integer id) {
		Result ret = new Result();
		IdleBaseVo idleBaseVo =new IdleBaseVo();
		idleBaseVo.setId(id);
		idleBaseVo.setStatus(IdleBaseStatusDictEnum.NORMAL.getCode());
		idleBaseService.updateById(idleBaseVo);
		return ret;
	}
	
	/**
	 * 删除
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/unclaimed")
	@ResponseBody
	public Result unclaimed(Integer id) {
		Result ret = new Result();
		IdleBaseVo idleBaseVo =new IdleBaseVo();
		idleBaseVo.setId(id);
		idleBaseVo.setStatus(IdleBaseStatusDictEnum.DELETE.getCode());
		idleBaseService.updateById(idleBaseVo);
		return ret;
	}
	
	/**
	 * 显示
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/isShow")
	@ResponseBody
	public Result isShow(Integer id) {
		Result ret = new Result();
		IdleBaseVo idleBaseVo =new IdleBaseVo();
		idleBaseVo.setId(id);
		idleBaseVo.setIsShow(YesNoEnum.YES.getCode());
		idleBaseService.updateById(idleBaseVo);
		return ret;
	}
	
	/**
	 * 不显示
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/myShow")
	@ResponseBody
	public Result myShow(Integer id) {
		Result ret = new Result();
		IdleBaseVo idleBaseVo =new IdleBaseVo();
		idleBaseVo.setId(id);
		idleBaseVo.setIsShow(YesNoEnum.NO.getCode());
		idleBaseService.updateById(idleBaseVo);
		return ret;
	}
	
	
}
